home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / siod / siod_v28.lha / siod.doc < prev    next >
Encoding:
Text File  |  1993-08-16  |  19.8 KB  |  583 lines

  1.  *                   COPYRIGHT (c) 1988-1992 BY                             *
  2.  *        PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  3.  *        See the source file SLIB.C for more information.                  *
  4.  
  5. Documentation for Release 2.7 17-MAR-92, George Carrette
  6.  
  7. [Release Notes:]
  8.  
  9. 1.4 This release is functionally the same as release 1.3 but has been
  10. remodularized in response to people who have been encorporating SIOD
  11. as an interpreted extension language in other systems.
  12.  
  13. 1.5 Added the -g flag to enable mark-and-sweep garbage collection.
  14.     The default is stop-and-copy.
  15.  
  16. 2.0 Set_Repl_Hooks, catch & throw. 
  17.  
  18. 2.1 Additions to SIOD.SCM: Backquote, cond.
  19.  
  20. 2.2 User Type extension. Read-Macros. (From C-programmer level).
  21.  
  22. 2.3 save-forms. load with argument t, comment character, faster intern.
  23.     -o flag gives obarray size. default 100.
  24.  
  25. 2.4 speed up arithmetic and the evaluator. fixes to siod.scm. no_interrupt
  26.     around calls to C I/O. gen_readr.
  27.  
  28. 2.5 numeric arrays in siod.c
  29.  
  30. 2.6 remodularize .h files, procedure prototypes. gc, eval, print hooks
  31.     now table-driven.
  32.  
  33. 2.7 hash tables, fasload.
  34.  
  35. gjc@paradigm.com
  36. George Carrette
  37.  
  38.    
  39. Paradigm Associates Inc          Phone: 617-492-6079
  40. 29 Putnam Ave, Suite 6
  41. Cambridge, MA 02138
  42.  
  43. [Files:]
  44.  
  45.  siod.h    Declarations 
  46.  siodp.h   private declarations.
  47.  slib.c    scheme library.
  48.  siod.c    a main program.
  49.  siod.scm  Some scheme code
  50.  pratt.scm A pratt-parser in scheme.
  51.  
  52.  
  53. [Motivation:]
  54.  
  55. The most obvious thing one should notice is that this lisp implementation 
  56. is extremely small. For example, the resulting binary executable file 
  57. on a VAX/VMS system with /notraceback/nodebug is 17 kilo-bytes.
  58.  
  59. Small enough to understand, the source file slib.c is 30 kilo-bytes.
  60.  
  61. Small enough to include in the smallest applications which require
  62. command interpreters or extension languages.
  63.  
  64. We also want to be able to run code from the book "Structure and
  65. Interpretation of Computer Programs." 
  66.  
  67. Techniques used will be familiar to most lisp implementors.  Having
  68. objects be all the same size, and having only two statically allocated
  69. spaces simplifies and speeds up both consing and gc considerably.  the
  70. MSUBR hack allows for a modular implementation of tail recursion,     
  71. an extension of the FSUBR that is, as far as I know, original.
  72. The optional mark and sweep garbage collector may be selected at runtime.
  73.  
  74. Error handling is rather crude. A topic taken with machine fault,
  75. exception handling, tracing, debugging, and state recovery which we
  76. could cover in detail, but is clearly beyond the scope of this
  77. implementation. Suffice it to say that if you have a good symbolic
  78. debugger you can set a break point at "err" and observe in detail all
  79. the arguments and local variables of the procedures in question, since
  80. there is no casting of data types. For example, if X is an offending
  81. or interesting object then examining X->type will give you the type,
  82. and X->storage_as.cons will show the car and the cdr.
  83.  
  84. [Invocation:]
  85.  
  86. siod [-hXXXXX] [-iXXXXX] [-gX] [-oXXXXX] [-nXXXXX] [-sXXXXX]
  87.  -h where XXXXX is an integer, to specify the heap size, in obj cells,
  88.  -i where XXXXX is a filename to load before going into the repl loop.
  89.  -g where X = 1 for stop-and-copy GC, X = 0 for mark-and-sweep.
  90.  -o where XXXXX is the size of the symbol hash table to use, default 100.
  91.  -n where XXXXX is the number of preconsed/interned non-negative numbers.
  92.  -s where XXXXX is the number of bytes of machine recursion stack.
  93.  
  94.   Example:
  95.    siod -isiod.scm -h100000
  96.  
  97. [Garbage Collection:]
  98.  
  99. There are two storage management techniques which may be chosen at runtime
  100. by specifying the -g argument flag. 
  101.  
  102.  -g1 (the default) is stop-and-copy. This is the simplest and most
  103.      portable implementation. GC is only done at toplevel.
  104.  -g0 is mark-and-sweep. GC is done at any time, required or requested.
  105.      However, the implementation is not as portable.
  106.  
  107. Discussion of stop-and-copy follows:
  108.  
  109. As one can see from the source, garbage collection is really quite an easy
  110. thing. The procedure gc_relocate is about 25 lines of code, and
  111. scan_newspace is about 15.
  112.  
  113. The real tricks in handling garbage collection are (in a copying gc):
  114.  (1) keeping track of locations containing objects
  115.  (2) parsing the heap (in the space scanning)
  116.  
  117. The procedure gc_protect is called once (e.g. at startup) on each
  118. "global" location which will contain a lisp object.
  119.  
  120. That leaves the stack. Now, if we had chosen not to use the argument
  121. and return-value passing mechanism provided by the C-language
  122. implementation, (also known as the "machine stack" and "machine
  123. procedure calling mechanism) this lisp would be larger, slower, and
  124. rather more difficult to read and understand. Furthermore it would be
  125. considerably more painful to *add* functionality in the way of SUBR's
  126. to the implementation.
  127.  
  128. Aside from writing a very machine and compiler specific assembling language
  129. routine for each C-language implementation, embodying assumptions about
  130. the placement choices for arguments and local values, etc, we
  131. are left with the following limitation: "YOU CAN GC ONLY AT TOP-LEVEL"
  132.  
  133. However, this fits in perfectly with the programming style imposed in
  134. many user interface implementations including the MIT X-Windows Toolkit.
  135. In the X Toolkit, a callback or work procedure is not supposed to spend
  136. much time implementing the action. Therefore it cannot have allocated
  137. much storage, and the callback trampoline mechanism can post a work
  138. procedure to call the garbage collector when needed.
  139.  
  140. Our simple object format makes parsing the heap rather trivial.
  141. In more complex situations one ends up requiring object headers or markers
  142. of some kind to keep track of the actual storage lengths of objects
  143. and what components of objects are lisp pointers.
  144.  
  145. Because of the usefulness of strings, they were added by default into
  146. SIOD 2.6. The implementation requires a hook that calls the C library
  147. memory free procedure when an object is in oldspace and never
  148. got relocated to newspace. Obviously this slows down the mark-and-sweep
  149. GC, and removes one of the usual advantages it has over mark-and-sweep.
  150.  
  151. Discussion of mark-and-sweep follows:
  152.  
  153. In a mark-and-sweep GC the objects are not relocated. Instead
  154. one only has to LOOK at objects which are referenced by the argument
  155. frames and local variables of the underlying (in this case C-coded)
  156. implementation procedures. If a pointer "LOOKS" like it is a valid
  157. lisp object (see the procedure mark_locations_array) then it may be marked,
  158. and the objects it points to may be marked, as being in-use storage which
  159. is not linked into the freelist in the gc_sweep phase.
  160.  
  161. Another advantage of the mark_and_sweep storage management technique is
  162. that only one heap is required.
  163.  
  164. This main disadvantages are:
  165. (1) start-up cost to initially link freelist.
  166.     (can be avoided by more general but slower NEWCELL code).
  167. (2) does not COMPACT or LOCALIZE the use of storage. This is poor engineering
  168.     practice in a virtual memory environment.
  169. (2) the entire heap must be looked at, not just the parts with useful storage.
  170.  
  171. In general, mark-and-sweep is slower in that it has to look at more
  172. memory locations for a given heap size, however the heap size can
  173. be smaller for a given problem being solved. More complex analysis
  174. is required when READ-ONLY, STATIC, storage spaces are considered.
  175. Additionally the most sophisticated stop-and-copy storage management
  176. techniques take into account considerations of object usage temporality.
  177.  
  178. The technique assumes that all machine registers the GC needs to
  179. look at will be saved by a setjmp call into the save_regs_gc_mark data.
  180.  
  181. [Compilation:]
  182.  
  183. This code (version 2.7) has been compiled and run under the following:
  184. - SUN-IV,      GCC (GNU C)
  185. - VAX/VMS,     VAXC
  186. - MacIntosh,   THINK C 5.0
  187.  
  188. Earlier versions were compiled and run on the AMIGA, Encore, and 4.3BSD.
  189. There are reports that the code will also compile and run under MS-DOS.
  190.  
  191. On all unix machines use (with floating-point flags as needed)
  192.   
  193.   %cc -O -c siod.c
  194.   %cc -O -c slib.c
  195.   %cc -O -c sliba.c
  196.   %cc -o siod siod.o slib.o sliba.o
  197.  
  198. If cc doesn't work, try gcc (GNU C, Free Software Foundation, Cambridge MA).
  199.  
  200. on VAX/VMS:
  201.  
  202.   $ cc siod
  203.   $ cc slib
  204.   $ cc sliba
  205.   $ link siod,slib,sliba,sys$input:/opt
  206.   sys$library:vaxcrtl/share
  207.   $ siod == "$" + F$ENV("DEFAULT") + "SIOD"
  208.  
  209. on AMIGA 500, ignore warning messages about return value mismatches,
  210.   %lc siod.c
  211.   %lc slib.c
  212.   %lc sliba.c
  213.   %blink lib:c.o,siod.o,slib.o,sliba.o to siod lib lib:lcm.lib,lib:lc.lib,lib:amiga.lib
  214.  
  215. in THINK C.
  216.   The siod project must include siod.c,slib.c,slib.c,sliba.c,siodm.c, ANSI.
  217.   The compilation option "require prototypes" should be used.
  218.  
  219. [System:]
  220.  
  221. The interrupts called SIGINT and SIGFPE by the C runtime system are
  222. handled by invoking the lisp error procedure. SIGINT is usually caused
  223. by the CONTROL-C character and SIGFPE by floating point overflow or underflow.
  224.  
  225. [Syntax:]
  226.  
  227. The only special characters are the parenthesis and single quote.
  228. Everything else, besides whitespace of course, will make up a regular token.
  229. These tokens are either symbols or numbers depending on what they look like.
  230. Dotted-list notation is not supported on input, only on output.
  231.  
  232. [Special forms:]
  233.  
  234. The CAR of a list is evaluated first, if the value is a SUBR of type 9 or 10
  235. then it is a special form.
  236.  
  237. (define symbol value) is presently like (set! symbol value).
  238.  
  239. (define (f . arglist) . body) ==> (define f (lambda arglist . body))
  240.  
  241. (lambda arglist . body) Returns a closure.
  242.  
  243. (if pred val1 val2) If pred evaluates to () then val2 is evaluated else val1.
  244.  
  245. (begin . body) Each form in body is evaluated with the result of the last
  246. returned.
  247.  
  248. (set! symbol value) Evaluates value and sets the local or global value of
  249. the symbol.
  250.  
  251. (or x1 x2 x3 ...) Returns the first Xn such that Xn evaluated non-().
  252.  
  253. (and x1 x2 x3 ...) Keeps evaluating Xj until one returns (), or Xn.
  254.  
  255. (quote form). Input syntax 'form, returns form without evaluation.
  256.  
  257. (let pairlist . body) Each element in pairlist is (variable value).
  258. Evaluates each value then sets of new bindings for each of the variables,
  259. then evaluates the body like the body of a progn. This is actually
  260. implemented as a macro turning into a let-internal form.
  261.  
  262. (the-environment) Returns the current lexical environment.
  263.  
  264. [Macro Special forms:]
  265.  
  266. If the CAR of a list evaluates to a symbol then the value of that symbol
  267. is called on a single argument, the original form. The result of this
  268. application is a new form which is recursively evaluated.
  269.  
  270. [Built-In functions:]
  271.  
  272. These are all SUBR's of type 4,5,6,7, taking from 0 to 3 arguments
  273. with extra arguments ignored, (not even evaluated!) and arguments not
  274. given defaulting to (). SUBR's of type 8 are lexprs, receiving a list
  275. of arguments. Order of evaluation of arguments will depend on the
  276. implementation choice of your system C compiler.
  277.  
  278. consp cons car cdr set-car! set-cdr!
  279.  
  280. number? + - * / < > eqv?
  281. The arithmetic functions all take two arguments.
  282.  
  283. eq?, pointer objective identity. (Use eqv? for numbers.)
  284.  
  285. symbolconc, takes symbols as arguments and appends them. 
  286.  
  287. symbol?
  288.  
  289. symbol-bound? takes an optional environment structure.
  290. symbol-value also takes optional env.
  291. set-symbol-value also takes optional env.
  292.  
  293. env-lookup takes a symbol and an environment structure. If it returns
  294. non-nil the CAR will be the value of the symbol.
  295.  
  296. assq
  297.  
  298. read,print
  299.  
  300. eval, takes a second argument, an environment.
  301.  
  302. copy-list. Copies the top level conses in a list.
  303.  
  304. oblist, returns a copy of the list of the symbols that have been interned.
  305.  
  306. gc-status, prints out the status of garbage collection services, the
  307. number of cells allocated and the number of cells free. If given
  308. a () argument turns gc services off, if non-() turns gc services on.
  309. In mark-and-sweep storage management mode the argument only turns on
  310. and off verbosity of GC messages.
  311.  
  312. gc, does a mark-and-sweep garbage collection. If called with argument nil
  313. does not print gc messages during the gc.
  314.  
  315. load, given a filename (which must be a symbol, there are no strings)
  316. will read/eval all the forms in that file. An optional second argument,
  317. if T causes returning of the forms in the file instead of evaluating them.
  318.  
  319. save-forms, given a filename and a list of forms, prints the forms to the
  320. file. 3rd argument is optional, 'a to open the file in append mode.
  321.  
  322. quit, will exit back to the operating system.
  323.  
  324. error, takes a symbol as its first argument, prints the pname of this
  325. as an error message. The second argument (optional) is an offensive
  326. object. The global variable errobj gets set to this object for later
  327. observation.
  328.  
  329. null?, not. are the same thing.
  330.  
  331. *catch tag exp, Sets up a dynamic catch frame using tag. Then evaluates exp.
  332.  
  333. *throw tag value, finds the nearest *catch with an EQ tag, and cause it to
  334. return value.
  335.  
  336. [Procedures in main program siod.c]
  337.  
  338. cfib is the same as standard-fib. You can time it and compare it with
  339. standard-fib to get an idea of the overhead of interpretation.
  340.  
  341. vms-debug invokes the VMS debugger. The one optional argument is
  342. a string of vms-debugger commands. To show the current call
  343. stack and then continue execution:
  344.  
  345.     (vms-debug "set module/all;show calls;go") 
  346.  
  347. Or, to single step and run at the same time:
  348.  
  349.     (vms-debug "for i=1 to 100 do (STEP);go")
  350.  
  351. Or, to set up a breakpoint on errors:
  352.  
  353.     (vms-debug "set module slib;set break err;go")
  354.  
  355.  
  356. [Utility procedures in siod.scm:]
  357.  
  358. Shows how to define macros.
  359.  
  360. cadr,caddr,cdddr,replace,list.
  361.  
  362. (defvar variable default-value)
  363.  
  364. And for us old maclisp hackers, setq and defun, and progn, etc.
  365.  
  366. call-with-current-continuation
  367.  
  368. Implemented in terms of *catch and *throw. So upward continuations
  369. are not allowed.
  370.  
  371. A simple backquote (quasi-quote) implementation.
  372.  
  373. cond, a macro.
  374.  
  375. append
  376.  
  377. nconc
  378.  
  379. [A streams implementation:]
  380.  
  381. The first thing we must do is decide how to represent a stream.
  382. There is only one reasonable data structure available to us, the list.
  383. So we might use (<stream-car> <cache-flag> <cdr-cache> <cdr-procedure>)
  384.  
  385. the-empty-stream is just ().
  386.  
  387. empty-stream?
  388.  
  389. head
  390.  
  391. tail
  392.  
  393. cons-stream is a special form. Wraps a lambda around the second argument.
  394.  
  395. *cons-stream is the low-level constructor used by cons-stream.
  396.  
  397. fasload, fasldump. Take the obvious arguments, and are implemented
  398. in terms of fast-read and fast-print.
  399.  
  400. compile-file. 
  401.  
  402. [Arrays:]
  403.  
  404. (cons-array size [type]) Where [type] is double, long, string, lisp or nil.
  405. (aref array index)
  406. (aset array index value) 
  407.  
  408. fasload and fasdump are effective ways of storing and restoring numeric
  409. array data.
  410.  
  411. [Benchmarks:]
  412.  
  413. A standard-fib procedure is included in siod.scm so that everyone will
  414. use the same definition in any reports of speed. Make sure the return
  415. result is correct. use command line argument of
  416.  %siod -h100000 -isiod.scm
  417.  
  418. (standard-fib 10) => 55 ; 795 cons work.
  419. (standard-fib 15) => 610 ; 8877 cons work.
  420. (standard-fib 20) => 6765 ; 98508 cons work.
  421.  
  422. [Porting:]
  423.  
  424. See the #ifdef definition of myruntime, which
  425. should be defined to return a double float, the number of cpu seconds
  426. used by the process so far. It uses the the tms_utime slot, and assumes
  427. a clock cycle of 1/60'th of a second.
  428.  
  429. If your system or C runtime needs to poll for the interrupt signal
  430. mechanism to work, then define INTERRUPT_CHECK to be something
  431. useful.
  432.  
  433. The STACK_LIMIT and STACK_CHECK macros may need to be conditionized.
  434. They currently assume stack growth downward in virtual address.
  435. The subr (%%stack-limit setting non-verbose) may be used to change the
  436. limits at runtime.
  437.  
  438. The stack and register marking code used in the mark-and-sweep GC
  439. is unlikely to work on machines that do not keep the procedure call
  440. stack in main memory at all times. It is assumed that setjmp saves
  441. all registers into the jmp_buff data structure.
  442.  
  443. If the stack is not always aligned (in LISP-PTR sense) then the 
  444. gc_mark_and_sweep procedure will not work properly. 
  445.  
  446. Example, assuming a byte addressed 32-bit pointer machine:
  447.  
  448. stack_start_ptr: [LISP-PTR(4)] 
  449.                  [LISP-PTR(4)]
  450.                  [RANDOM(4)]
  451.                  [RANDOM(2)]
  452.                  [LISP-PTR(4)]
  453.                  [LISP-PTR(4)]
  454.                  [RANDOM(2)]
  455.                  [LISP-PTR(4)]
  456.                  [LISP-PTR(4)]
  457. stack_end:       [LISP-PTR(4)]
  458.  
  459. As mark_locations goes from start to end it will get off proper alignment
  460. somewhere in the middle, and therefore the stack marking operation will
  461. not properly identify some valid lisp pointers.
  462.  
  463. Fortunately there is an easy fix to this. A more aggressive use of
  464. our mark_locations procedure will suffice.
  465.  
  466. For example, say that there might be 2-byte random objects inserted into
  467. the stack. Then use two calls to mark_locations:
  468.  
  469.  mark_locations(((char *)stack_start_ptr) + 0,((char *)&stack_end) + 0);
  470.  mark_locations(((char *)stack_start_ptr) + 2,((char *)&stack_end) + 2);
  471.  
  472. If we think there might be 1-byte random objects, then 4 calls are required:
  473.  
  474.  mark_locations(((char *)stack_start_ptr) + 0,((char *)&stack_end) + 0);
  475.  mark_locations(((char *)stack_start_ptr) + 1,((char *)&stack_end) + 1);
  476.  mark_locations(((char *)stack_start_ptr) + 2,((char *)&stack_end) + 2);
  477.  mark_locations(((char *)stack_start_ptr) + 3,((char *)&stack_end) + 3);
  478.  
  479.  
  480. [Interface to other programs:]
  481.  
  482. If your main program does not want to actually have a read/eval/print
  483. loop, and instead wants to do something else entirely, then use
  484. the routine set_repl_hooks to set up for procedures for:
  485.  
  486.  * putting the prompt "> " and other info strings to standard output.
  487.  
  488.  * reading (getting) an expression
  489.  
  490.  * evaluating an expression
  491.  
  492.  * printing an expression.
  493.  
  494. The routine get_eof_val may be called inside your reading procedure
  495. to return a value that will cause exit from the read/eval/print loop.
  496.  
  497. In order to call a single C function in the context of the repl loop,
  498. you can do the following:
  499.  
  500. int flag = 0;
  501.  
  502. void my_puts(st)
  503.  char *st;
  504. {}
  505.  
  506. LISP my_reader()
  507. {if (flag == 1)
  508.   return(get_eof_val());
  509.  flag == 1;
  510.  return(NIL);}
  511.  
  512. LISP my_eval(x)
  513.  LISP x;
  514. {call_my_c_function();
  515.  return(NIL);}
  516.  
  517. LISP my_print(x)
  518.  LISP x;
  519. {}
  520.  
  521. do_my_c_function()
  522. {set_repl_hooks(my_puts,my_read,my_eval,my_print);
  523.  repl_driver(1, /* or 0 if we do not want lisp's SIGINT handler */
  524.              0);}
  525.  
  526.  
  527. If you need a completely different read-eval-print-loop, for example
  528. one based in X-Window procedures such as XtAddInput, then you may want to
  529. have your own input-scanner and utilize a read-from-string kind of
  530. function.
  531.  
  532.  
  533. [User Type Extension:]
  534.  
  535. There are 5 user types currently available. tc_user_1 through tc_user_5.
  536. If you use them then you must at least tell the garbage collector about
  537. them. To do this you must have 4 functions,
  538.  * a user_relocate, takes a object and returns a new copy.
  539.  * a user_scan, takes an object and calls relocate on its subparts.
  540.  * a user_mark, takes an object and calls gc_mark on its subparts or
  541.                 it may return one of these to avoid stack growth.
  542.  * a user_free, takes an object to hack before it gets onto the freelist.
  543.  
  544. set_gc_hooks(type,
  545.              user_relocate_fcn,
  546.              user_scan_fcn,
  547.              user_mark_fcn,
  548.              user_free_fcn,
  549.              &kind_of_gc);
  550.  
  551. kind_of_gc should be a long. It will receive 0 for mark-and-sweep, 1 for
  552. stop-and-copy. Therefore set_gc_hooks should be called AFTER process_cla.
  553. You must specify a relocate function with stop-and-copy. The scan
  554. function may be NULL if your user types will not have lisp objects in them.
  555. Under mark-and-sweep the mark function is required but the free function
  556. may be NULL.
  557.  
  558. See SIOD.C for a very simple string-append implementation example.
  559.  
  560. You might also want to extend the printer. This is optional.
  561.  
  562. set_print_hooks(type,fcn);
  563.  
  564. The fcn receives the object which should be printed to its second
  565. argument, a FILE*.
  566.  
  567. The evaluator may also be extended, with the "application" of user defined
  568. types following in the manner of an MSUBR.
  569.  
  570. Lastly there is a simple read macro facility.
  571.  
  572. void set_read_hooks(char *all_set,char *end_set,
  573.             LISP (*fcn1)(),LISP (*fcn2)())
  574.  
  575. All_set is a string of all read macros. end_set are those
  576. that will end the current token.
  577.  
  578. The fcn1 will receive the character used to trigger
  579. it and the struct gen_readio * being read from. It should return a lisp object.
  580.  
  581. the fnc2 is optional, and is a user hook into the token => lisp object
  582. conversion.
  583.